package com.krt.game01;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Color;

public class Button {
	int m_x1, m_y1, m_x2, m_y2;
	int m_height, m_width;
	int m_centerX = 0;
	int m_centerY = 0;
	
	int m_value;
	String m_text;
	boolean m_touched;
	boolean m_visible;
	boolean m_isSpecial;

	int m_colorBoxFillNotSel = Color.GRAY;
	int m_colorBoxFillSel = Color.GREEN;
	
	int m_colStroke = Color.BLACK;
	
	int m_colTextSel = Color.BLACK;
	int m_colTextNotSel = Color.BLACK;
	
	Rect m_rect = new Rect();
	Paint m_paintRect = new Paint();
	Paint m_paintText = new Paint();
	
	
	public Button(int left, int top, int right, int bottom, int value)
	{
		setCoords(left, top, right, bottom);
		
		m_value = value;
		
		m_touched = false;
		m_visible = true;
	}
	
	public Button()
	{
		m_x1 = 0;
		m_y1 = 0;
		m_x2 = 0;
		m_y2 = 0;
		m_value = 0;
		
		m_height = 0;
		m_width = 0;
		
		m_touched = false;
		m_visible = true;
	}
	
	
	
	
	public void setCoords(int left, int top, int right, int bottom){
		try {
			if (left > right) {
			      Exception myException = new Exception("left coordinate > right coordinate");
			      throw myException;		
			}
			if (top > bottom) {
			      Exception myException = new Exception("top coordinate > bottom coordinate");
			      throw myException;		
			}			
			
			m_x1 = left;
			m_y1 = top;
			m_x2 = right;
			m_y2 = bottom;
			m_height = calcHeight();
			m_width = calcWidth();
			m_rect.left = m_x1;
			m_rect.top = m_y1;
			m_rect.right = m_x2;
			m_rect.bottom = m_y2;
			m_centerX = left + (m_width/2);
			m_centerY = top + (m_height/2);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public int getCenterX(){
		return m_centerX;
	}
	
	public int getCenterY(){
		return m_centerY;
	}
	
	public void setValue(int value){
		m_value = value;
	}
	
	public int getValue(){
		return m_value;
	}
	
	public String getValueString(){
		return String.valueOf(m_value);
	}

	private int calcHeight(){
		return m_y2 - m_y1;
	}
	
	private int calcWidth(){
		return m_x2 - m_x1;
	}
	
	public int getHeight() {
		return m_height;
	}
	
	public int getWidth() {
		return m_width;
	}
	
	public boolean getTouched(){
		return m_touched;
	}
	
	public boolean isVisible(){
		return m_visible;
	}
	
	public void setVisibility(boolean visible){
		m_visible = visible;
	}
	
	public void setTouched(boolean touched){
		m_touched = touched;
	}
	
	public Rect getRect(){
		return m_rect;		
	}
	
	public void draw(Canvas canvas){
		//draw box (FILL)
		m_paintRect.setStyle(Paint.Style.FILL);
		if (m_touched==false) {
			m_paintRect.setColor(m_colorBoxFillNotSel);
			//System.out.println("touched: false");
			
			//new button
			canvas.drawBitmap(MainActivity.g_bmp_button_grey, null, m_rect, m_paintRect);
			
		}
		else if (m_touched==true) {
			m_paintRect.setColor(m_colorBoxFillSel);
//			Log.d("Button.draw", "touched: true");
			
			//new button
			canvas.drawBitmap(MainActivity.g_bmp_button_green, null, m_rect, m_paintRect);
		}
		
		//old buttons
		//canvas.drawRect(m_rect, m_paintRect);
		
		//new buttons
		//canvas.drawBitmap(MainActivity.g_bmp_button_grey, null, m_rect, m_paintRect);
		
/*
		//draw frame (STROKE) around box
		m_paintRect.setStyle(Paint.Style.STROKE);
		m_paintRect.setColor(m_colStroke);
		m_paintRect.setStrokeWidth(0);

		canvas.drawRect(m_rect, m_paintRect);
*/ 
		//draw text
		
		m_paintText.setColor(m_colTextSel);
		m_paintText.setTextAlign(Paint.Align.CENTER);
		m_paintText.setTextSize((int)(m_height*0.7));
		Rect bounds = new Rect();
		
		if (m_isSpecial == true) {
			m_paintText.getTextBounds(m_text, 0, m_text.length(), bounds);
			canvas.drawText(m_text, m_x1+m_width/2, m_y1+m_height/2+(bounds.bottom-bounds.top)/2, m_paintText);
		}
		else if (m_isSpecial == false) {
			m_paintText.getTextBounds(getValueString(), 0, getValueString().length(), bounds);
			canvas.drawText(getValueString(), m_x1+m_width/2, m_y1+m_height/2+(bounds.bottom-bounds.top)/2, m_paintText);
		}
		
	


	}
	
	public boolean contains(int x, int y){
		boolean tempbool = m_rect.contains(x, y);
//		m_touched = tempbool;
		return tempbool;
	}
	
}
